package kr.hkit.powerjava.chapter12;

import java.util.Arrays;
import java.util.Comparator;

class Student{ //implements Comparable<Student> {
	private String stdName;
	private double gpa;

	public Student(String stdName, double gpa) {
		this.stdName = stdName;
		this.gpa = gpa;
	}

	public String getStdName() {
		return stdName;
	}

	public double getGpa() {
		return gpa;
	}

	@Override
	public String toString() {
		return String.format("[%s, %s]", stdName, gpa);
	}
/*
	@Override
	public int compareTo(Student o) {
		// TODO Auto-generated method stub
		return (int) (this.gpa - o.gpa);
	}*/

}

public class SortTest {
	public static void main(String[] args) {
		int[] arr = { 5, 6, 7, 8, 1, 2, 3, 4 };
		prnArr(arr);
		
		double[][] m = new double[2][];
		
		

		Arrays.sort(arr);
		prnArr(arr);

		Student[] arrStd = new Student[4];
		arrStd[0] = new Student("홍길동", 3.39);
		arrStd[1] = new Student("임꺽정", 4.21);
		arrStd[2] = new Student("황진이", 2.19);
		arrStd[3] = new Student("김태희", 1.4);

		prnArr(arrStd);
		// Arrays.sort(arrStd);//compareTo의 -1 0 1의 값으로 정렬
		//이름 순 정렬
		Comparator<Student> compName = new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				return o1.getStdName().hashCode() - o2.getStdName().hashCode();
				//hashcode = 문자의 숫자화
			}
		};
		
		//평점순 정렬
		Comparator<Student> compGpa = new Comparator<Student>() {
			
			@Override
			public int compare(Student o1, Student o2) {
				return (int) (o1.getGpa()*10 - o2.getGpa()*10);
			}
		};
		
		Arrays.sort(arrStd, compName);//뒤에 있는 값의 compare값을 가지고 정렬
		prnArr(arrStd);
		
		Arrays.sort(arrStd, compGpa);//뒤에 있는 값의 compare값을 가지고 정렬
		prnArr(arrStd);
	}

	private static void prnArr(Student[] arrStd) {
		for (Student s : arrStd) {
			System.out.print(s + "\t");
		}
		System.out.println();
	}

	private static void prnArr(int[] arr) {
		for (int e : arr) {
			System.out.printf("%2d", e);
		}
		System.out.println();
	}
}
